﻿Imports System.Data
Imports Mysoft.Map.Data
Partial Public Class Order_Sh_Result_Grid
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim arrParam() As String = Request.Form("shdate").Split("|")
            Dim strOrderguid As String = Request.Form("txtOrderGuidlst")
            Dim isSuccess As Boolean
            Dim iSuccess As Integer
            Dim iFail As Integer
            Dim strsql As String
            Dim dt As DataTable
            Dim strTableName As String
            Dim strFkField As String
            If arrParam(0) = "定单" Then
                strTableName = "s_Order"
                strFkField = "orderguid"
            Else
                strTableName = "s_contract"
                strFkField = "contractguid"
            End If

            strsql = "UPDATE " & strTableName & " SET IsValid=1,AuditBy='" & arrParam(1) & "',AuditingDate='" & arrParam(2) & "',LastMender='" & arrParam(3) & "',ModiDate='" & arrParam(4) & "' WHERE " & strFkField & " in ('" & strOrderguid.Replace(",", "','") & "') and  (status is null or status='' or (status is not null and status<>'关闭')) and not exists(" & _
            "select TOP 1 1 from s_SaleModiApply " & _
            " where (ApplyState<>'作废' or ApplyState is null) and ((approvestate<>'已审批' or approvestate is null) or (approvestate='已审批' and (ApplyState<>'已执行' or ApplyState is null))) AND SaleGUID=" & strTableName & "." & strFkField & ")"

            Try
                iSuccess = MyDB.ExecSQL(strsql)
                isSuccess = True
            Catch ex As Exception
                strsql = "select '' as rowid,  roomguid,roominfo,'数据库错误！' as error from ep_room where roomguid in (select roomguid from " & strTableName & " where " & strFkField & " in ('" & strOrderguid.Replace(",", "','") & "'))"
                dt = MyDB.GetDataTable(strsql)
                isSuccess = False
                iSuccess = 0
                iFail = dt.Rows.Count
            End Try

            If isSuccess = True Then
                strsql = "select '' as rowid, ep_room.roomguid,ep_room.roominfo,case when  " & strTableName & ".status='关闭' then '该房间对应的" & arrParam(0) & "正被他人占用！'  else '该房间对应的" & arrParam(0) & "中存在未结束业务变更的记录！' end as error from ep_room inner join  " & strTableName & " on  ep_room.roomguid=" & strTableName & ".roomguid where  " & strFkField & " in ('" & strOrderguid.Replace(",", "','") & "') and  ( " & strTableName & ".status='关闭' or exists(" & _
                                       "select TOP 1 1 from s_SaleModiApply " & _
                                       " where (ApplyState<>'作废' or ApplyState is null) and ((approvestate<>'已审批' or approvestate is null) or (approvestate='已审批' and (ApplyState<>'已执行' or ApplyState is null))) AND SaleGUID=" & strTableName & "." & strFkField & "))"
                dt = MyDB.GetDataTable(strsql)
                iFail = dt.Rows.Count
            End If

            '自定义控件绑定数据
            Repeater1.BindMode = 1                                          '绑定模式：0 -- SQL 语句绑定，1 -- DataTable 绑定
            Repeater1.PKField = "RoomGUID"
            Repeater1.DataTable = dt                                          '设置查询语句
            Repeater1.DataBind()

            Page.RegisterStartupScript("KKKK", "<script language=javascript>alert('成功审核" & iSuccess.ToString & "条记录，失败" & iFail.ToString & "条记录！');parent.document.all.txtSuccessCnt.value=" & iSuccess.ToString & ";</script>")
        End If
    End Sub
End Class